package com.liushuo.dao;

import com.liushuo.entity.Question;
import com.liushuo.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class QuestionDao {
    public void save(List<Question> questions, Connection conn) throws SQLException {
        String sql = "insert into questions(question_text,answer,has_parentheses) values(?,?,?)";
        try (PreparedStatement ps = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS)) {
            for (Question question : questions) {
                ps.setString(1, question.getQuestionText());
                ps.setInt(2, question.getAnswer());
                ps.setBoolean(3, question.isHasParentheses());

                ps.addBatch();
            }
            int[] rowsAffected = ps.executeBatch();
            try (ResultSet rs = ps.getGeneratedKeys()) {
                int index = 0;
                for (int i = 0; i < rowsAffected.length; i++) {
                    if (rowsAffected[i] > 0) {
                        while (rs.next()) {
                            int id = rs.getInt(1);
                            questions.get(index).setId(id);
                            index++;
                        }
                    }
                }
            }
        }
    }

    public List<Question> getQuestionsByIds(List<Integer> ids) {
        List<Question> questions = new ArrayList<>();
        if (ids.isEmpty()) {
            return questions;
        }
        // 构建动态 SQL 语句，使用 UNION ALL 来模拟重复 id 查询
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ids.size(); i++) {
            if (i > 0) {
                sb.append(" UNION ALL ");
            }
            sb.append("SELECT * FROM questions WHERE id = ?");
        }
        String sql = sb.toString();

        try {
            Connection conn = DBUtil.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            for (int i = 0; i < ids.size(); i++) {
                ps.setInt(i + 1, ids.get(i));
            }
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Question question = Question.builder()
                        .id(rs.getInt("id"))
                        .questionText(rs.getString("question_text"))
                        .answer(rs.getInt("answer"))
                        .hasParentheses(rs.getBoolean("has_parentheses"))
                        .build();
                questions.add(question);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return questions;
    }


    public List<Question> getAll(){
        String sql = "select * from questions";
        List<Question> questions = new ArrayList<>();
        try {
            Connection conn = DBUtil.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Question question = Question.builder()
                        .id(rs.getInt("id"))
                        .questionText(rs.getString("question_text"))
                        .answer(rs.getInt("answer"))
                        .hasParentheses(rs.getBoolean("has_parentheses"))
                        .build();
                questions.add(question);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return questions;
    }

}
